<?php
/**
 *
 * Author: luffyzhao@vip.126.com
 * DateTime: 2019/4/3 16:12
 */

namespace App\Http\Controllers\Manage;


use App\Http\Requests\Manage\Auth\LoginRequest;
use App\Http\Requests\Manage\Auth\UpdatePasswordRequest;
use App\Http\Searchs\Manage\ProfileLogSearch;
use App\Models\ManageUser;
use App\Repositories\ManageMenu as Menu;
use App\Repositories\ManageUser as User;
use Illuminate\Auth\AuthenticationException;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
    /**
     * @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function getAuth()
    {
        return auth('manage');
    }

    /**
     * login
     * @param LoginRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws AuthenticationException
     * @author luffyzhao@vip.126.com
     */
    public function login(LoginRequest $request, Menu $menu)
    {
        $token = $this->getAuth()->attempt($request->only([
            'email', 'password'
        ]));
        $user = $this->getAuth()->user();
        if (!$token || !$user->allowLogin()) {
            throw new AuthenticationException('用户不存在，或者用户被禁用');
        }

        if ($user->role_id === null) {
            $menus = $menu->get(['id', 'name', 'parent_id', 'title', 'icon']);
        } else {
            $menus = $user->role->cachedMenus();
        }


        return $this->response([
            'token' => (string)$token,
            'menus' => $menus,
            'exp' => $token->getTime()
        ]);
    }

    /**
     * logout
     * @author luffyzhao@vip.126.com
     */
    public function logout()
    {
        return $this->response($this->getAuth()->logout());
    }

    /**
     * refresh
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function refresh()
    {
        return $this->response((string)$this->getAuth()->refresh());
    }

    /**
     * profile
     * @author luffyzhao@vip.126.com
     */
    public function profile()
    {
        $user = $this->getAuth()->user();
        $user->load(['role']);
        return $this->response($user);
    }

    /**
     * @param ProfileLogSearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function logs(ProfileLogSearch $search){
        /** @var ManageUser $user */
        $user = $this->getAuth()->user();
        return $this->response(
            $user->logs()->where($search->toArray())->paginate(request()->input('per_page'))
        );
    }

    /**
     * profileUpdate
     * @param User $user
     * @param UpdatePasswordRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function password(User $user, UpdatePasswordRequest $request)
    {
        return $this->response(
            $user->password(
                $this->getAuth()->user(),
                $request->only([
                    'password'
                ])
            )
        );
    }
}
